package com.zero.guava.samples.baseDemo;

import org.apache.curator.shaded.com.google.common.base.Strings;

/**
 * String实例
 */
public class StringDemo {

    private static final String string = "test";//测试用字符串

    public static void main(String[] args) {

        original();
        testString();
    }

    public static void original(){

        StringBuilder sb = new StringBuilder(string);
        char c = 's';
        for (int i = 0; i < 6 - string.length(); i++) {
            sb.append(c);
        }
        System.out.println(sb.toString());
    }
    
    public static void testString(){

        //guava中提供了padEnd（param1,length,param2）方法
        //将param1的长度改变为length，长度不足的话向后追加字符param2
        String padEnd = Strings.padEnd(string, 6, 's');
        System.out.println(padEnd);

        //这里如果param1的长度大于length，则默认返回原字符串param1
        String padEnd2 = Strings.padEnd(string, 2, 's');
        System.out.println(padEnd2);

        //padStart()方法，向前追加，长度超出默认返回原字符
        String padStart = Strings.padStart(string, 6, 's');
        System.out.println(padStart);

        String padStart1 = Strings.padStart(string, 2, 's');
        System.out.println(padStart1);

        //nullToEmpty：将null值转为空字符串
        String nullToEmpty = Strings.nullToEmpty(null);
        System.out.println(nullToEmpty);

        //emptyToNull：将空字符串转为null
        String emptyToNull = Strings.emptyToNull("");
        System.out.println(emptyToNull);

        //isNullOrEmpty：判断字符串为null或空字符串
        boolean nullOrEmpty = Strings.isNullOrEmpty("");
        System.out.println(nullOrEmpty);

        //repeat：用于将指定字符串循环拼接多次返回
        String repeat = Strings.repeat(string, 3);
        System.out.println(repeat);

        //commonPrefix：返回两个字符串中相同的前缀部分
        String commonPrefix = Strings.commonPrefix("nihaoma?", "nibuhaoma?");
        System.out.println(commonPrefix);

        //commonSuffix：返回两个字符串中相同的后缀部分
        String commonSuffix = Strings.commonSuffix("nihaoma?", "nibuhaoma?");
        System.out.println(commonSuffix);
    }
}
